﻿// --------------------------------
// <copyright file="AddConstantForm.cs" company="TRONSoft">
//     Copyright (c) TRONSoft.
// </copyright>
// <license>
//   This source code is subject to terms and conditions of the Microsoft
//   Public License. A copy of the license can be found in the License.html
//   file at the root of this distribution. If you cannot locate the
//   Microsoft Public License, please send an email to dlr@microsoft.com.
//   By using this source code in any fashion, you are agreeing to be bound 
//   by the terms of the Microsoft Public License. You must not remove this
//   notice, or any other, from this software.
// </license>
// <author>Tron</author>
// ---------------------------------

using System;
using System.ComponentModel;
using System.Globalization;
using Tronsoft.Actucal.CalculationEngineCreator;
using WeifenLuo.WinFormsUI.Docking;

namespace Actucal
{
    public partial class AddConstantForm : DockContent
    {
        public AddConstantForm()
        {
            InitializeComponent();

            ProductsService.CurrentProductChanged += ProductsService_CurrentProductChanged;
        }

        public string NameOfConstant
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }

        public object ValueOfConstant
        {
            get { return txtValue.Text; }
            set
            {
                rbDouble.Checked = (value.GetType() == typeof(double));
                rbInteger.Checked = (value.GetType() == typeof(int));
                rbString.Checked = (value.GetType() == typeof(string));

                txtValue.Text = Convert.ToString(value, CultureInfo.InvariantCulture);
            }
        }

        public INameValueCombination Constant
        {
            get
            {
                if (rbString.Checked)
                {
                    return new NameValueCombination<string>(txtName.Text, txtValue.Text);
                }
                else if (rbInteger.Checked)
                {
                    int result;
                    int.TryParse(txtValue.Text, out result);

                    return new NameValueCombination<int>(txtName.Text, result);
                }
                else
                {
                    double result;
                    double.TryParse(txtValue.Text, out result);

                    return new NameValueCombination<double>(txtName.Text, result);
                }
            }
        }

        private void ProductsService_CurrentProductChanged(object sender, ArgumentEventArgs<Product> e)
        {
            Enabled = (ProductsService.CurrentProduct != null);
        }

        public static event EventHandler ConstantAdded;

        protected static void OnConstantAdded(object sender, EventArgs e)
        {
            if (null != ConstantAdded)
                ConstantAdded(sender, e);
        }

        public void Reset()
        {
            txtName.Text = txtValue.Text = string.Empty;
            rbString.Checked = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ValidateChildren();

            int index = ProductsService.CurrentProduct.Constants.FindIndex(
                delegate(INameValueCombination constantParam)
                    {
                        return
                            (constantParam.Name == Constant.Name);
                    });

            bool constantExists = (index != -1);

            if (!constantExists)
            {
                ProductsService.CurrentProduct.Constants.Add(Constant);
                OnConstantAdded(this, new EventArgs());
            }
            else
            {
                // TODO: Solve the constants dilemma. work around
                ProductsService.CurrentProduct.Constants.RemoveAt(index);
                ProductsService.CurrentProduct.Constants.Add(Constant);
            }
        }

        private void txtName_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                e.Cancel = true;
                errorProvider1.SetError(txtName, "Vul een naam in.");
            }
        }

        private void txtValue_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(txtValue.Text))
            {
                e.Cancel = true;
                errorProvider1.SetError(txtValue, "Vul een waarde in.");
            }
        }

        private void AddConstantForm_Activated(object sender, EventArgs e)
        {
        }
    }
}